﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml;
using Microsoft.Xna.Framework.Content;
using System.Collections;

namespace _2D
{
    /// <summary>
    /// Esta clase carga los Tiles especificados en el XML del mapa.
    /// </summary>
    public class Map
    {
        private Texture2D texture;
        private Vector2 position;
        private XmlDocument mapXML;
        private string tilesFile;
        private int[][] matrix;

        public Map()
        {
        }

        public void Load(ContentManager content, string textureName, string XMLFile)
        {
            texture = content.Load<Texture2D>(textureName);
            mapXML = new XmlDocument();
            mapXML.Load(XMLFile);
            tilesFile = textureName;

            XmlNodeList map = mapXML.GetElementsByTagName("map");

            XmlNodeList BaseLayer =
            ((XmlElement)map[0]).GetElementsByTagName("BaseLayer");
             

            ArrayList alMapTiles = new ArrayList();

            foreach (XmlElement nodo in BaseLayer)  // Sólo hay uno.
            {
                string tilesOrder = nodo.InnerText;
                char[] separators = new char[1];
                separators[0] = ';';
                string[] tilesSplit1 = tilesOrder.Split(separators, StringSplitOptions.RemoveEmptyEntries);


                int row = 0;
                int col;
                matrix = new int[tilesSplit1.Count()][];

                foreach (string tileRow in tilesSplit1)
                {
                    string[] tilesSplit2 = tileRow.Split(' ');
                    col = 0;

                    int[] aux = new int[tilesSplit2.Count()];
                    foreach (string tileColumn in tilesSplit2)
                    {

                        aux[col] = int.Parse(tileColumn);

                        col++;
                    }
                    matrix[row] = aux;
                    row++;
                }

            }
        }

        public void Draw(SpriteBatch batch, Tile[] tiles)
        {
            for (int row = 0; row < matrix.Length; row++)
            {
                for (int col = 0; col < matrix[row].Length; col++)
                {
                    int tileCode = matrix[row][col];
                    position = new Vector2(col * tiles[tileCode].tsTile.tileWidth, row * tiles[tileCode].tsTile.tileHeight);

                    Rectangle sourcerect = new Rectangle(tiles[tileCode].tsTile.tileX, tiles[tileCode].tsTile.tileY,
                        tiles[tileCode].tsTile.tileWidth, tiles[tileCode].tsTile.tileHeight);


                    batch.Draw(texture, position, sourcerect, Color.White,
                        0, new Vector2(0, 0), 1, SpriteEffects.None, 1);
                }
            }
        }
    }
}

